// pages/information/information.js
const app = getApp()
var request = require("../../utils/request.js");
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    hasUserInfo: false,
    phone: '',
    user: {},
    verifyLoginDialog: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let token = wx.getStorageSync('lmtc_token');
    if (token) {
      this.getUserInfo();
      this.setData({
        hasbind: '已绑定'
      })
    } else {
      this.setData({
        hasbind: '未绑定',
        verifyLoginDialog: true
      })
    }
  },
  verifyLogin() {
    this.getUserInfo()
    this.setData({
      hasbind: '已绑定'
    })
  },
  /**
   * 获取手机号
   * @param {*} e 
   */
  getPhoneNumber: function (e) {
    var that = this;
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      let encryptedData = e.detail.encryptedData;
      let iv = e.detail.iv;
      wx.login({
        success(res) {
          let url = '/customer/info/bindMobilePhone'
          let params = {
            code: res.code,
            encryptedData: encryptedData,
            iv: iv
          }
          request.http(url, 'GET', params)
            .then(res => {
              if (res.data.code == 200) {
                let phone = res.data.data
                phone = phone.substring(0, 3) + '****' + phone.substring(7);
                Toast.success('绑定成功');
                that.setData({
                  'user.mobilePhone': phone
                })
              }
            })
        }
      })
    }
  },
  /**
   * 获取用户信息
   */
  getUserInfo() {
    let that = this;
    let url = '/customer/info/getCustomerInfo'
    request.http(url, 'GET')
      .then((res) => {
        if (res.data.code == 200) {
          let user = res.data.data;
          if (user != null && user != undefined) {
            let phone = user.mobilePhone;
            if (phone != null && phone != undefined && phone != '') {
              phone = phone.substring(0, 3) + '****' + phone.substring(7);
              that.setData({
                hasUserInfo: true,
                user: user,
                'user.mobilePhone': phone
              })
            } else {
              that.setData({
                hasUserInfo: true,
                user: user,
                phone: ''
              })
            }
          }
        }
      })
  }
})